﻿using Sustainalytics.Utils;
using System;
using System.Diagnostics.Tracing;
using System.Threading;

namespace Sustainalytics.Portfolios.BL
{
    [EventSource(Name = "Sustainalytics-ClientPlatform-PortfolioManagement")]
    public sealed class PortfolioDetailsManagementEventSource : EventSource
    {
        private static Lazy<PortfolioDetailsManagementEventSource> _lazyLog = new Lazy<PortfolioDetailsManagementEventSource>(InitLog, LazyThreadSafetyMode.ExecutionAndPublication);

        public static PortfolioDetailsManagementEventSource Log
        {
            get
            {
                return _lazyLog.Value;
            }
        }

        [NonEvent]
        public static PortfolioDetailsManagementEventSource InitLog()
        {
            var log = new PortfolioDetailsManagementEventSource();
            log.Ping();

            return log;
        }

        [Event(1, Level = EventLevel.LogAlways)]
        public void Ping()
        {
            if (IsEnabled())
                WriteEvent(1);
        }

        [NonEvent]
        public void PortfolioError(string info, Exception exception)
        {
            if (exception == null)
                return;

            if (IsEnabled())
            {
                PortfolioError(info, exception.GetType().ToString(), exception.Message, ExceptionUtility.GetInfo(exception, true));
            }
        }

        [Event(1001, Level = EventLevel.Error, Message = "Portfolio/Portfolio/Error")]
        public void PortfolioError(string info, string exceptionType, string exceptionMesage, string exceptionInformation)
        {
            if (IsEnabled())
            {
                WriteEvent(1001, info, exceptionType, exceptionMesage, exceptionInformation);
            }
        }
    }
}
